﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using System.Net;
using System.IO;

namespace Tools
{
    public class HttpClient
    {
        public static void Post(string data, string url, Dictionary<string, string> httpHeaders)
        {
            HttpResponseMessage response = null;
            StringContent httpContent = null;

            if (data != null)
            {
                httpContent = new StringContent(data, new UTF8Encoding(), "application/json");
            }

            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                if (httpHeaders != null)
                {
                    if (httpContent != null)
                    {
                        foreach (var headerItem in httpHeaders)
                        {
                            httpContent.Headers.Add(headerItem.Key, headerItem.Value);
                        }
                    }
                    else
                    {
                        foreach (var headerItem in httpHeaders)
                        {
                            client.DefaultRequestHeaders.Add(headerItem.Key, headerItem.Value);
                        }
                    }
                }
                response = client.PostAsync(url, httpContent).Result;
                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception(response.StatusCode + response.ReasonPhrase);
                }
            }
        }
        public static string Get(string data, string url, Dictionary<string, string> httpHeaders)
        {
            HttpResponseMessage response = null;
            StringContent httpContent = null;

            if (data != null)
            {
                httpContent = new StringContent(data, new UTF8Encoding(), "application/json");
            }

            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                if (httpHeaders != null)
                {
                    if (httpContent != null)
                    {
                        foreach (var headerItem in httpHeaders)
                        {
                            httpContent.Headers.Add(headerItem.Key, headerItem.Value);
                        }
                    }
                    else
                    {
                        foreach (var headerItem in httpHeaders)
                        {
                            client.DefaultRequestHeaders.Add(headerItem.Key, headerItem.Value);
                        }
                    }
                }
                response = client.GetAsync(url).Result;
                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception(response.StatusCode + response.ReasonPhrase);
                }

                return response.Content.ReadAsStringAsync().Result;
            }
        }
        public static string Get(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
    }
}

